<template>
  <div class="app-container">
    <el-form ref="form" label-width="120px" :model="form">
      <el-col :span="24">
        <el-form-item label="工位:" prop="vin">
            <el-select style="width: 100%" v-model="form.ulocNo" auto-complete="off" placeholder="请选择工位">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="VIN:" prop="vin">
          <el-input ref="vin" type="search" v-model="form.vin" @keyup.native.enter="listBindRecord($event)"/>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="序列号:" prop="scanTrackNo">
          <el-input ref="scanTrackNo" v-model="form.scanTrackNo" @keyup.native.enter="bind($event)"/>
        </el-form-item>
      </el-col>
    </el-form>
    <div v-if="bandingDetail">
      <div class="tag-group">
        <el-col :span="24">
          <h3 class="tag-group__title">未绑定关键件</h3>
          <el-tag
            v-for="tag in unBindKeyParts"
            :key="tag"
            style="margin: 10px"
            :type="'danger'"
            effect="plain"
          >{{ tag }}
          </el-tag>
        </el-col>
      </div>
      <div class="tag-group">
        <el-col :span="24">
          <h3 class="tag-group__title">已绑定关键件</h3>
          <el-tag
            v-for="tag in bindKeyParts"
            :key="tag"
            style="margin: 10px"
            :type="'success'"
            effect="plain"
          >{{ tag }}
          </el-tag>
        </el-col>
      </div>
    </div>

  </div>
</template>

<script>

import { listBindRecord, bind } from '@/api/mes/vehicleControl/batteryBind'

export default {
  name: 'BatteryBind',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表单参数
      form: {
        ulocNo: undefined,
        vin: undefined,
        scanTrackNo: undefined
      },
      options: [{
        value: 'Z050',
        label: 'Z050'
      }],
      bindKeyParts: [],
      unBindKeyParts: [],
      bandingDetail:true,
    }
  },
  created() {
    this.form.ulocNo = this.options[0].value
  },
  methods: {
    listBindRecord(event) {
      const param = { vin: this.form.vin, ulocNo: this.form.ulocNo }
      listBindRecord(param).then(response => {
          this.bindKeyParts = response.data.bindKeyParts
          this.unBindKeyParts = response.data.unBindKeyParts
        if (event){
          event.target.blur()
          this.$refs.scanTrackNo.focus()
        }
      })
    },
    bind(event) {
      bind(this.form).then(response => {
        this.msgSuccess('绑定成功')
        this.listBindRecord()
        this.form.vin = undefined
        this.form.scanTrackNo = undefined
        if (event){
          event.target.blur()
          this.$refs.vin.focus()
        }
      })
    },
    // 刷新绑定数据
    reload () {
      this.isReloadData = false
      this.$nextTick(() => {
        this.isReloadData = true
      })
    }
  }
}
</script>


